package com.ferryc.fegin.hystrix;

import com.ferryc.exception.BaseException;
import com.ferryc.fegin.PayFeignClient;
import com.ferryc.model.Result;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Created by Ferry Chen on 2019/11/13.
 */
@Component
@Slf4j
public class PayServiceFallbackFactory implements FallbackFactory<PayFeignClient> {


    @Override
    public PayFeignClient create(Throwable throwable) {
        log.error("fallBack,Reason:" + throwable.getMessage());
        return new PayFeignClient() {
            @Override
            public Result add(String userId, Double money) {
                return Result.fail("保存失败");
            }

            @Override
            public Result consume(String userId, Double money) {
                if (throwable instanceof BaseException) {
                    return Result.fail(throwable.getMessage());
                }
                return Result.fail("处理失败");
            }

            @Override
            public Result update(String userId, Double money) {
                return Result.fail("消费失败");
            }

            @Override
            public Result checkBalance(String userId, Double money) {
                return Result.fail("查询失败");
            }
        };
    }
}
